# ============================================================================
# Table 6: Effect of source name within each policy condition
# ============================================================================

# --- Race-conscious policy condition ---
data_pol <- filter(.data = data, police == 1)
diff_police <- mean(data_pol$support_policy[data_pol$deandre == 1]) -
  mean(data_pol$support_policy[data_pol$deandre == 0])
mean_police <- mean(data_pol$support_policy[data_pol$deandre == 0])
sd_police <- sqrt(mean((data_pol$support_policy[data_pol$deandre == 0] - mean_police)^2))
N_police <- nrow(data_pol)

n_deandre_police <- sum(data_pol$deandre)
set.seed(seed = 09291992, kind = "L'Ecuyer-CMRG")
Omega_police <- obtain_permutation_matrix(declaration = declare_ra(N = N_police,
                                                                   m = n_deandre_police),
                                          maximum_permutations = n_sims)
null_police <- sapply(X = 1:ncol(Omega_police),
                        FUN = function(x) { mean(data_pol$support_policy[Omega_police[,x] == 1]) -
                            mean(data_pol$support_policy[Omega_police[,x] == 0])})
pval_police <- min(1, 2 * min(mean(null_police <= diff_police), mean(null_police >= diff_police)))

# --- poverty-conscious policy condition ---
data_poverty <- filter(.data = data, police == 0)
diff_poverty <- mean(data_poverty$support_policy[data_poverty$deandre == 1]) -
  mean(data_poverty$support_policy[data_poverty$deandre == 0])
mean_poverty <- mean(data_poverty$support_policy[data_poverty$deandre == 0])
sd_poverty <- sqrt(mean((data_poverty$support_policy[data_poverty$deandre == 0] - mean_poverty)^2))
N_poverty <- nrow(data_poverty)

n_deandre_poverty <- sum(data_poverty$deandre)
set.seed(seed = 09291992, kind = "L'Ecuyer-CMRG")
Omega_poverty <- obtain_permutation_matrix(declaration = declare_ra(N = N_poverty,
                                                                   m = n_deandre_poverty),
                                          maximum_permutations = n_sims)
null_poverty <- sapply(X = 1:ncol(Omega_poverty),
                        FUN = function(x) { mean(data_poverty$support_policy[Omega_poverty[,x] == 1]) -
                            mean(data_poverty$support_policy[Omega_poverty[,x] == 0])})
pval_poverty <- min(1, 2 * min(mean(null_poverty <= diff_poverty), mean(null_poverty >= diff_poverty)))

diff_police_str <- fmt(x = diff_police, digits = 4, pval = pval_police)
diff_poverty_str <- fmt(x = diff_poverty, digits = 4, pval = pval_poverty)

mean_police_str <- fmt(x = mean_police, digits = 4)
mean_poverty_str <- fmt(x = mean_poverty, digits = 4)

sd_police_str <- fmt(x = sd_police, digits = 4)
sd_poverty_str <- fmt(x = sd_poverty, digits = 4)

pval_police_str <- fmt(x = pval_police, digits = 4)
pval_poverty_str <- fmt(x = pval_poverty, digits = 4)

N_police_str <- format_int(x = N_police)
N_poverty_str <- format_int(x = N_poverty)

table_6 <- sprintf(fmt = "\\caption{Estimated effects of Black source vs.~White source on policy support by policy type}
\\centering
\\begin{tabular}{@{\\extracolsep{5pt}}lcc} 
\\\\[-1.8ex]\\hline 
\\hline \\\\[-1.8ex] 
 & \\multicolumn{2}{c}{\\textit{Dependent variable:}} \\\\ 
\\cline{2-3} 
\\\\[-1.8ex] & \\multicolumn{2}{c}{Policy Support} \\\\ 
\\\\[-1.8ex] & Race-conscious & poverty-conscious \\\\ 
\\hline \\\\[-1.8ex] 
Black Source Cue & $%s$ & $%s$ \\\\ 
White source cue (control) mean & $%s$ & $%s$ \\\\ 
White source cue (control) SD & $%s$ & $%s$ \\\\ 
RI $p$-value (two-sided) & $%s$ & $%s$ \\\\ 
Number of observations & $%s$ & $%s$ \\\\ 
\\hline 
\\hline 
\\textit{Note:}  & \\multicolumn{2}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\\\ 
\\end{tabular}",
                   diff_police_str,
                   diff_poverty_str,
                   mean_police_str,
                   mean_poverty_str,
                   sd_police_str,
                   sd_poverty_str,
                   pval_police_str,
                   pval_poverty_str,
                   N_police_str,
                   N_poverty_str
)

writeLines(text = table_6, con = "code_and_output/tables/table_6.tex")

rm(list = setdiff(ls(), c("data", "fmt", "format_int", "n_sims")))
gc()